# -*- coding: utf-8 -*-
from datetime import timedelta
from utils.operators.spark_submit import SparkSubmitOperator
from contextlib import ExitStack

jms_dwd__dwd_sign_aging_detail_dt = SparkSubmitOperator(
    task_id='jms_dwd__dwd_sign_aging_detail_dt',
    email=['wangmenglei@jtexpress.com','yl_bigdata@yl-scm.com'],
    pool_slots=4,
    # depends_on_past=True,  # 如果任务依赖于前一天的同名任务，则将 depends_on_past 设为 True
    task_concurrency=1,  # 如果任务不支持并发，则将 task_concurrency 设为 1
    name='jms_dwd__dwd_sign_aging_detail_dt_{{ execution_date | date_add(1) | cst_ds }}',  # yarn 任务名称
    driver_memory='8G',
    driver_cores=2,
    executor_memory='16G',
    executor_cores=4,
    num_executors=20,
    conf={'spark.dynamicAllocation.enabled': 'true',  # 动态资源开启
          'spark.shuffle.service.enabled': 'true',  # 动态资源 Shuffle 服务开启
          'spark.dynamicAllocation.maxExecutors': 30,  # 动态资源最大扩容 Executor 数
          'spark.dynamicAllocation.cachedExecutorIdleTimeout': 180,  # 动态资源自动释放闲置 Executor 的超时时间(s)
          'spark.executor.memoryOverhead': 2048,
          'spark.core.connection.ack.wait.timeout': 300,
          'spark.default.parallelism': 300,
          'spark.locality.wait': 60,
          },
    # jars='hdfs:///user/oozie/chenhoukun/report/signed_duration_data/common-1.0-SNAPSHOT.jar',  # 依赖 jar 包
    jars='hdfs:///scheduler/jms/spark/chk/report/signed_duration_data/common-1.0-SNAPSHOT.jar',  # 依赖 jar 包
    java_class='com.yunlu.bigdata.jobs.report.SignAgingDetailData',  # spark 主类
    # application='hdfs:///user/oozie/chenhoukun/report/signed_duration_data/pro/original-jobs-1.0-SNAPSHOT.jar',  # spark jar 包
    application='hdfs:///scheduler/jms/spark/chk/report/signed_duration_data/pro/original-jobs-1.0-SNAPSHOT.jar',
    # spark jar 包
    application_args=['{{ execution_date | date_add(1) | cst_ds }}'],  # 参数dt 运行当天日期
    #execution_timeout=timedelta(hours=1)
    #excel平均时长:9分34秒
    execution_timeout = timedelta(minutes=30),
)

# 设置上游依赖
def set_deps():
    from jms.dwd.tab.dwd_tab_reback_transfer_express_base import jms_dwd__dwd_tab_reback_transfer_express_base
    from jms.dwd import jms_dwd__dwd_yl_oms_oms_waybill_incre_dt
    from jms.dws import jms_dws__dws_actual_taking_dt, jms_dws__dws_actual_aging_sign_dt
    from jms.ods import jms_ods__tab_barscan_centersend, jms_ods__tab_barscan_centerarrival, \
    jms_ods__yl_oms_interceptorpiece, \
    jms_ods__tab_end_piece
# 设置依赖
    jms_dwd__dwd_sign_aging_detail_dt << [
        jms_dwd__dwd_yl_oms_oms_waybill_incre_dt,
        jms_dws__dws_actual_taking_dt,
        jms_dws__dws_actual_aging_sign_dt,
        jms_ods__tab_barscan_centersend,
        jms_ods__tab_barscan_centerarrival,
        jms_ods__yl_oms_interceptorpiece,
        jms_dwd__dwd_tab_reback_transfer_express_base,
        jms_ods__tab_end_piece,
]

# 延迟执行设置上游依赖，避免 import python 包的时候其他 Operator 尚未加载完成引起的循环依赖错误
with ExitStack() as stack:
    stack.callback(set_deps)